package com.caihappy.controller;

import com.caihappy.pojo.User;
import com.caihappy.service.IUserService;
import com.springmvc.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by DELL on 2022/4/2
 **/
@Controller
public class UserController {


    //持有业务逻辑层的对象
    @Autowired
    IUserService userservice;

    @RequestMapping("/users/query")
    public void findUsers(HttpServletRequest request, HttpServletResponse response, @RequestParam("username") String name) throws IOException {
        //处理相应的中文乱码问题
        response.setContentType("text/html;charset=utf-8");
        List<User> users = userservice.findUsers(name);
        PrintWriter out = response.getWriter();
        out.println("<h1>SpringMvc控制器" + name + "</h1>");
    }

    @RequestMapping("/users/get_msg")
    public String getMessage(HttpServletRequest request, HttpServletResponse response, String name) {
        response.setContentType("text/html;charset=utf-8");
        String userMessage = userservice.getUserMessage(name);
        request.setAttribute("userMessage", userMessage);
        return "/User.jsp";
    }

    @RequestMapping("/users/query_json")
    @ResponseBody //返回值自动转为JSON
    public List<User> queryUsers(HttpServletRequest request, HttpServletResponse response, String name) {
        return userservice.findUsers(name);
    }
}
